<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <title>购货单记录-库存管理</title>
</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">

<div class="layui-body" style="padding:15px;top: 0px;">

    <!-- start sp-wrapper -->
    <link rel="stylesheet" href="/sb/stock.css"/>
    <div class="sp-wrapper">
        <h2 class="sp-title"><span>退货入库</span></h2>
        <form action="/stock/order_stockout_list" method="get">
            <div class="sp-tool">
            <span class="fl"><!--<b>状态：<select>
                    <option>待出库</option>
                    <option>已出库</option>
                </select></b>--><b>日期：<input type="text" th:value="${startDate}" class="date"> - <input type="text"
                                                                                                        th:value="${endDate}"
                                                                                                        class="date"></b><b><input
                    type="text" name="billNo" th:value="${billNo}"><input type="submit" value="查询"></b></span>
            </div>
        </form>
        <div class="sp-table">
            <table>
                <thead>
                <tr>
                    <th>序号</th>
                    <th>单据日期</th>
                    <th>入库单编号</th>
                    <th>订单编号</th>
                    <th>退款金额</th>
                    <th>数量</th>
                    <th>状态</th>
                    <th>操作人</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="sp-data">
                <tr th:each="item:${list}">
                    <td>1</td>
                    <td th:text="${item.billDate}">2019-03-15</td>
                    <td th:text="${item.billNo}">XS201903260932042</td>
                    <td th:text="${item.getSrcOrderNo()}">190325194916938</td>
                    <td th:text="${#numbers.formatDecimal(item.amount, 0, 2)}">1000.00</td>
                    <td th:text="${item.getTotalQuantity()}">900</td>
                    <td th:text="*{item.getChecked()==0} ? '待出库' : '已出库'">待出库</td>
                    <td th:text="${item.checkName}">张三</td>
                    <td><i class="view" th:text="*{item.getChecked()==0} ? '入库' : '查看'"
                           th:attr="data-id=${item.getId()},data-stats=${item.getChecked()},data-srcId=${item.getSrcOrderId()},d-date=${item.getBillDate()},d-date2=${item.getModifyTime()},d-no=${item.billNo}">查看</i>
                    </td>
                </tr>
                </tbody>
            </table>
            <div class="pages">
                <paging:pager th:value="${pageIndex}" th:rows="${totalSize}" th:size="${pageSize}"/>
            </div>
        </div>
    </div>

    <!-- 模板 查看 -->
    <script type="text/template" id="tpl-1">
        <div class="ck-box">
            <p>
                <b>退货单编号：<em id="ck-ckdbh"></em></b>
                <b>订单编号：<em id="ck-ddbh"></em></b>
                <b>用户手机号：<em id="ck-yhsjh"></em></b>
                <b>状态：<em id="ck-zt">待出库</em></b>
            </p>
            <p>
                <b>收货人姓名: <em id="ck-shrxm"></em></b>
                <b>收货人地址: <em id="ck-shrdz"></em></b>
                <b>单据日期: <em id="ck-djrq"></em></b>
                <b>入库日期: <em id="ck-ckrq"></em></b>
            </p>
            <div class="sp-table">
                <form id="stockOut" method="post" action="/stock/stock_order_in">
                    <input type="hidden" name="orderId" id="order_id">
                    <input type="hidden" name="erp_invoice_id" id="erpInvoiceId">
                    <table>
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>商品</th>
                            <th>数量</th>
                            <th>销售单价</th>
                            <th>退款金额</th>
                            <th>仓库</th>
                        </tr>
                        </thead>
                        <tbody id="ck-data">


                        </tbody>
                        <tfoot>
                        <td>&nbsp;</td>
                        <td><strong>合计</strong></td>
                        <td><strong id="ck-zjsl"></strong></td>
                        <td>&nbsp;</td>
                        <td><strong id="ck-zjje"></strong></td>
                        <td>&nbsp;</td>
                        </tfoot>
                    </table>
                </form>
            </div>
        </div>
    </script>

    <script type="text/javascript" src="/sb/jquery.min.js"></script>
    <script type="text/javascript" src="/sb/Tips.min.js"></script>
    <script type="text/javascript">
        $(function () {
            // 查看
            $('.view').click(function () {
                var srcid = $(this).attr('data-srcId');//订单id
                var id = $(this).attr('data-id');//id
                var status = $(this).attr('data-stats');//状态
                var date = $(this).attr('d-date');//单据日期
                var date2 = $(this).attr('d-date2');//出库日期
                var no = $(this).attr('d-no');//出库单编号
                var data = {
                    orderId: srcid,
                    erpInvoiceId: id
                };
                // $.ajax 完成弹出
                $.ajax({
                    url: "/stock_api/order_query",
                    type: "POST",
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (res) {
                        var data = res.data;
                        if (status == 0) {
                            Tips.alert({
                                title: '订单详情',
                                content: $('#tpl-1').html(),
                                defineText: '退货入库',
                                render: function () {
                                    $('#ck-ckdbh').text(no);
                                    $('#ck-ddbh').text(data.orderNum);
                                    $('#ck-yhsjh').text(data.consigneeMobile);
                                    $('#ck-zt').text("待入库");
                                    $('#ck-shrxm').text(data.consignee);
                                    $('#ck-shrdz').text(data.address);
                                    $('#ck-djrq').text(date);
                                    $('#order_id').val(srcid);
                                    $('#erpInvoiceId').val(id);
                                    var tpl = '', tpls = '';
                                    var dd = data.goodsDetail;
                                    for (var i = 0, n = dd.length; i < n; i++) {
                                        tpl += '<input type="hidden" name="erp_invoice_info_id" value=' + dd[i].id + '><tr>\
                                            <td>' + (i + 1) + '</td>\
                                            <td>' + dd[i].title + '</td>\
                                            <td>' + dd[i].quantity + '</td>\
                                            <td>' + dd[i].price + '</td>\
                                            <td>' + dd[i].price * dd[i].quantity + '</td>\
                                            <td><select name="stockId">' + tpls + '</select></td>\
                                            </tr>';
                                    }
                                    $('#ck-data').html(tpl);
                                    $('#ck-zjsl').text(data.goodsNum);
                                    $('#ck-zjje').text(data.paymentPrice);
                                },
                                define: function () {
                                    // 处理出库
                                    $('#stockOut').submit();
                                }
                            });
                        } else {
                            Tips.open({
                                title: '订单详情',
                                content: $('#tpl-1').html(),
                                defineText: '退货入库',
                                render: function () {
                                    var time = getDateTime(date2 * 1000);
                                    $('#ck-ckdbh').text(no);
                                    $('#ck-ddbh').text(data.orderNum);
                                    $('#ck-yhsjh').text(data.consigneeMobile);
                                    $('#ck-zt').text("已入库");
                                    $('#ck-shrxm').text(data.consignee);
                                    $('#ck-shrdz').text(data.address);
                                    $('#ck-djrq').text(date);
                                    $('#ck-ckrq').text(time);
                                    $('#order_id').val(srcid);
                                    var tpl = '';
                                    var dd = data.goodsDetail;
                                    for (var i = 0, n = dd.length; i < n; i++) {
                                        tpl += '<input type="hidden" name="erp_invoice_id" value=' + dd[i].id + '><tr>\
                                            <td>' + (i + 1) + '</td>\
                                            <td>' + dd[i].title + '</td>\
                                            <td>' + dd[i].quantity + '</td>\
                                            <td>' + dd[i].price + '</td>\
                                            <td>' + dd[i].price * dd[i].quantity + '</td>\
                                            <td>' + dd[i].locationName + '</td>\
                                            </tr>';
                                    }
                                    $('#ck-data').html(tpl);
                                    $('#ck-zjsl').text(data.goodsNum);
                                    $('#ck-zjje').text(data.paymentPrice);
                                }
                            });
                        }
                    }
                });
            });

            function getDateTime(timestamp) {
                var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
                var Y = date.getFullYear();
                var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
                var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
                var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours());
                var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes());
                var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
                return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
            }
        });
    </script>
    <!-- end sp-wrapper -->
    <script>
        layui.use(['laydate', 'layer'], function () {
            var laydate = layui.laydate;
            var layer = layui.layer;

            //执行一个laydate实例
            laydate.render({
                elem: '.date' //指定元素
            });
        });


        //页面层

        // layer.open({
        //     type: 1,
        //     skin: 'layui-layer-rim', //加上边框
        //     area: ['420px', '240px'], //宽高
        //     content: 'html内容'
        // });
    </script>
</div>
</body>
<html>
